<?php
session_start();
require_once '../config/database.php';
require_once '../includes/functions.php';

header('Content-Type: application/json');

// 检查管理员权限
if (!isset($_SESSION['user_id']) || $_SESSION['username'] !== 'admin') {
    echo json_encode(['success' => false, 'message' => '无权限']);
    exit;
}

try {
    // 当前轮次
    $current_round = getCurrentRound();
    
    // 当前价格
    $current_price = getCurrentPrice();
    
    // 今日交易次数
    $stmt = $pdo->query("SELECT COUNT(*) as today_trades FROM transactions WHERE DATE(transaction_time) = CURDATE()");
    $today_trades = $stmt->fetch()['today_trades'];
    
    // 今日交易金额
    $stmt = $pdo->query("SELECT SUM(amount) as today_volume FROM transactions WHERE DATE(transaction_time) = CURDATE()");
    $today_volume = $stmt->fetch()['today_volume'] ?? 0;
    
    // 活跃用户（今日有交易）
    $stmt = $pdo->query("SELECT COUNT(DISTINCT user_id) as active_users FROM transactions WHERE DATE(transaction_time) = CURDATE()");
    $active_users = $stmt->fetch()['active_users'];
    
    // 获取当前价格趋势设置
    $current_trend = $_SESSION['admin_price_trend'] ?? 'random';
    $current_strength = $_SESSION['admin_price_strength'] ?? 5;
    
    echo json_encode([
        'success' => true,
        'current_round' => $current_round['round_number'],
        'current_price' => $current_price,
        'today_trades' => $today_trades,
        'today_volume' => $today_volume,
        'active_users' => $active_users,
        'price_trend' => $current_trend,
        'price_strength' => $current_strength
    ]);
    
} catch (Exception $e) {
    echo json_encode([
        'success' => false,
        'message' => '获取统计数据失败: ' . $e->getMessage()
    ]);
}
?>
